<?php

namespace App\Http\Controllers\BusinessData;

use App\Http\Conf\ResultInfo;
use App\Http\Controllers\Controller;
use App\Http\Models\Business\Material;
use App\Http\Models\CategoryBig;
use App\Http\Models\CategoryContract;
use App\Http\Models\CategoryKeyProduct;
use App\Http\Models\CategorySmall;
use App\Http\Requests\Business\MaterialRequest;
use App\Imports\MaterialImport;
use Exception;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class MaterialController extends Controller
{
    private $select_where = [];
    private $request_params = [
        'year' => '',
        'is_special_offer' => 9999,
        'material' => '',
        'category_small_id' => 9999,
        'category_big_id' => 9999,
        'category_contract_id' => 9999,
        'category_key_product_id' => 9999,
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->dealRequestParams($request);

        // dd($this->select_where);
        $query = Material::where($this->select_where);

        if (isset($request->delete) && count($this->select_where) > 0) {
            $query->delete();
        }

        $category_smalls = CategorySmall::all();
        $category_bigs = CategoryBig::all();
        $category_contracts = CategoryContract::all();
        $category_key_products = CategoryKeyProduct::all();

        $models = $query->select([
            'id',
            'year',
            'material',
            'material_desc',
            'category_small_id',
            'category_big_id',
            'category_contract_id',
            'category_key_product_id',
            'is_special_offer'
        ])->paginate(config('report.quantity_per_page'));

        return view('business_data.material.index', [
            'models' => $models,
            'category_smalls' => $category_smalls,
            'category_bigs' => $category_bigs,
            'category_contracts' => $category_contracts,
            'category_key_products' => $category_key_products,
            'request_params' => $this->request_params,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $category_smalls = CategorySmall::all();
        $category_bigs = CategoryBig::all();
        $category_contracts = CategoryContract::all();
        $category_key_products = CategoryKeyProduct::all();
        return view('business_data.material.create', [
            'category_smalls' => $category_smalls,
            'category_bigs' => $category_bigs,
            'category_contracts' => $category_contracts,
            'category_key_products' => $category_key_products
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(MaterialRequest $request)
    {
        $model = new Material();
        $model->year = $request->year;
        $model->material = $request->material;
        $model->material_desc = $request->material_desc;
        $model->category_small_id = $request->category_small_id;
        $model->category_big_id = $request->category_big_id;
        $model->category_contract_id = $request->category_contract_id;
        $model->category_key_product_id = $request->category_key_product_id;
        $model->is_special_offer = $request->is_special_offer;
        if ($model->save()) {
            return redirect()->action('BusinessData\MaterialController@show', $model);
        } else {
            dd("添加数据有异常！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Material::find($id);
        return view('business_data.material.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $model = Material::find($id);
        $category_smalls = CategorySmall::all();
        $category_bigs = CategoryBig::all();
        $category_contracts = CategoryContract::all();
        $category_key_products = CategoryKeyProduct::all();
        return view('business_data.material.edit', [
            'model' => $model,
            'category_smalls' => $category_smalls,
            'category_bigs' => $category_bigs,
            'category_contracts' => $category_contracts,
            'category_key_products' => $category_key_products
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(MaterialRequest $request, $id)
    {
        $model = Material::find($id);
        $model->year = $request->year;
        $model->material = $request->material;
        $model->material_desc = $request->material_desc;
        $model->category_small_id = $request->category_small_id;
        $model->category_big_id = $request->category_big_id;
        $model->category_contract_id = $request->category_contract_id;
        $model->category_key_product_id = $request->category_key_product_id;
        $model->is_special_offer = $request->is_special_offer;
        if ($model->save()) {
            return redirect()->action('BusinessData\MaterialController@show', $model);
        } else {
            dd("编辑数据有异常！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = Material::find($id);
        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }

    public function import(Request $request)
    {
        if ($request->method() == 'GET') {
            return view('business_data.material.import');
        } elseif ($request->method() == 'POST') {
            if ($request->upload_file == null) {
                return redirect()->back()->with('fail', '请选择文件');
            }

            try {
                Excel::import(new MaterialImport, request()->file('upload_file'));
            } catch (Exception $e) {
                return redirect()->back()->with('fail', $e->getMessage());
            }
            return redirect()->action('BusinessData\MaterialController@index')->with('success', '导入数据成功！');
        }
    }

    /**
     * 处理请求参数
     */
    private function dealRequestParams(Request $request)
    {
        if (is_numeric($request->year)) {
            $this->select_where[] = ['year', $request->year];
            $this->request_params['year'] = $request->year;
        }

        if ($request->material != '') {
            $this->select_where[] = ['material', 'like', '%' . $request->material . '%'];
            $this->request_params['material'] = $request->material;
        }

        if ($request->category_small_id != 9999) {
            $this->select_where[] = ['category_small_id', $request->category_small_id];
            $this->request_params['category_small_id'] = $request->category_small_id;
        }

        if ($request->category_big_id != 9999) {
            $this->select_where[] = ['category_big_id', $request->category_big_id];
            $this->request_params['category_big_id'] = $request->category_big_id;
        }

        if ($request->category_contract_id != 9999) {
            $this->select_where[] = ['category_contract_id', $request->category_contract_id];
            $this->request_params['category_contract_id'] = $request->category_contract_id;
        }

        if ($request->category_key_product_id != 9999) {
            $this->select_where[] = ['category_key_product_id', $request->category_key_product_id];
            $this->request_params['category_key_product_id'] = $request->category_key_product_id;
        }

        if ($request->is_special_offer != 9999) {
            $this->select_where[] = ['is_special_offer', $request->is_special_offer];
            $this->request_params['is_special_offer'] = $request->is_special_offer;
        }
    }
}
